from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
import config

DATABASE_URL = config.DATABASE_URL

# 添加隔离级别设置
engine = create_engine(
    DATABASE_URL, 
    echo=True,
    isolation_level="REPEATABLE READ"  # 或"SERIALIZABLE"根据需求选择
)

engine = create_engine(DATABASE_URL, echo=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) #SQLAlchemy 的连接池机制,禁用了自动提交和自动刷新

Base = declarative_base()

def get_db():
    db = SessionLocal()  # 打开数据库连接
    try:
        yield db         # 把连接交给 FastAPI 管理
    finally:
        db.close()
